package com.think.archive.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.think.archive.dto.SysRoleDto;
import com.think.archive.dto.SysUserDto;
import com.think.archive.mapper.SysFuncMapper;
import com.think.archive.mapper.SysRoleFuncMapper;
import com.think.archive.mapper.SysRoleMapper;
import com.think.archive.model.SysFunc;
import com.think.archive.model.SysFuncExample;
import com.think.archive.model.SysRole;
import com.think.archive.model.SysRoleExample;
import com.think.archive.service.SysFuncService;
import com.think.archive.service.SysRoleService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class SysRoleServiceImpl  implements SysRoleService {

    @Autowired
    SysRoleMapper roleMapper;

    @Autowired
    SysRoleFuncMapper roleFuncMapper;

    @Autowired
    SysFuncMapper funcMapper;

    @Override
    public SysRole select(int id){
        SysRole role=roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public SysRole selectByRoleCode(String roleCode) {
        SysRoleExample example=new SysRoleExample();
        example.createCriteria().andRoleCodeEqualTo(roleCode);
        List<SysRole> roles=roleMapper.selectByExample(example);
        if(roles.isEmpty()){
            return null;
        }else{
            return roles.get(0);
        }
    }

    @Override
    public List<SysRoleDto> selectAllRole() {
        List<SysRole> roles=roleMapper.selectByExample(null);
        List<SysRoleDto> roleDtos=transUserToDtos(roles);
        return roleDtos;
    }

    @Override
    public int insertOrUpdateRole(SysRole role) {
        SysRole roleSave=roleMapper.selectByPrimaryKey(role.getId());
        if(roleSave==null){
            role.setCreater("system");
            role.setCreateTime(new Date());
            return roleMapper.insert(role);
        }
        else{
            roleSave.setRoleName(role.getRoleName());
            roleSave.setStatus(role.getStatus());
            roleSave.setSort(role.getSort());
            roleSave.setDescribed(role.getDescribed());
            roleSave.setRemark(role.getRemark());
            return roleMapper.updateByPrimaryKey(roleSave);
        }
    }

    @Override
    public int deleteRole(int id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int changeStatusRole(int id, String status) {
        SysRole role=roleMapper.selectByPrimaryKey(id);
        role.setStatus(status);
        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public List<SysFunc> roleFunc(String roleCode) {
        List<SysFunc> roleFunc=roleFuncMapper.roleFunc(roleCode);
        return roleFunc;
    }

    @Override
    public SysRoleDto transUserToDto(SysRole role) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        SysRoleDto roleDto = mapperFactory.getMapperFacade().map(role, SysRoleDto.class);
        List<SysFunc> funcs=roleFuncMapper.roleFunc(role.getRoleCode());
        roleDto.setFuncs(funcs);
        return roleDto;
    }

    @Override
    public List<SysRoleDto> transUserToDtos(List<SysRole> roles) {
        List<SysRoleDto> roleDtos=new ArrayList<>();
        for(SysRole role:roles){
            SysRoleDto roleDto=transUserToDto(role);
            roleDtos.add(roleDto);
        }
        return roleDtos;
    }

    @Override
    public JSONArray findRoleTree(String roleCode) {
        JSONArray jsonArray=findNextTree("",roleCode);
        return jsonArray;
    }

    @Override
    public List<SysRole> findNextLevelRole(String roleCode) {
        SysRoleExample example=new SysRoleExample();
        SysRoleExample.Criteria criteria=example.createCriteria();
        criteria.andRoleCodeLike(roleCode+"__");
        List<SysRole> roles=roleMapper.selectByExample(example);
        return roles;
    }

    @Override
    public List<SysRoleDto> findNextLevelRoleDto(String roleCode) {
        List<SysRole> roles=findNextLevelRole(roleCode);
        return transUserToDtos(roles);
    }

    public JSONArray findNextTree(String nodeCode,String roleCode){
        JSONArray leaf=new JSONArray();
        List<SysRole> roles=findNextLevelRole(nodeCode);
        for(SysRole role:roles){
            JSONObject node=new JSONObject();
            node.put("text",role.getRoleName());
            node.put("code",role.getRoleCode());
            node.put("href","/admin/role/list?roleCode="+role.getRoleCode());
            JSONObject state=new JSONObject();
            JSONArray jsonArray=findNextTree(role.getRoleCode(),roleCode);
            if(roleCode.startsWith(role.getRoleCode())){
                state.put("expanded",true);
            }else{
                state.put("expanded",false);
            }
            if(roleCode.equals(role.getRoleCode())){
                state.put("selected",true);
            }else{
                state.put("selected",false);
            }
            node.put("state",state);
            if(jsonArray.size()>0){
                node.put("nodes",jsonArray);
            }
            leaf.add(node);
        }
        return leaf;
    }
}
